layui.use(['form','layer','laydate','table','laytpl'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //文章列表
    var tableIns = table.render({
        elem: '#newsList',
        url : 'searchAllArticleIsCheck',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limit : 10,
        limits : [10,15,20,25],
        id : "newsListTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50,},
            {field: 'articleId', title: '文章编号', width:60, align:"center",},
            {field: 'title', title: '文章标题', width:200,edit:"text"},
            {field: 'userName', title: '发布者', align:'center'},
            {field: 'acheck', title: '发布状态',  align:'center',templet:"#newsStatus"},
            {field: 'content', title: '具体内容', event: 'detail',align:'center',edit:"text"},
            {field: 'articleDate', title: '发表日期', align:'center'},
            {field: 'articlePicture', title: '发表图片', align:'center',templet:function (data) {
                if (data.articlePicture != null || data.articlePicture != ""){
                    return "<img src='../../"+ data.articlePicture+"'>";
                } else {
                    return "没有封面";
                }
                }},
            {field: 'simContent', title: '文章简介', align:'center',edit:"text"},
            {title: '操作', width:170, templet:'#newsListBar',fixed:"right",align:"center"}
        ]]
    });

    $(".refresh_btn").click(function(){
        tableIns.reload();
    })

    //列表操作
    table.on('tool(newsList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //通过
            var check = $.ajax({
                url : "checkArticle",
                type : "get",
                data : {articleId : data.articleId}
            });
            check.done(function (result) {
                if (result.code == 100){
                    tableIns.reload();
                    setTimeout(function(){
                        layer.msg("审核通过");
                    },500);

                } else {

                }
            })
        } else if(layEvent === 'look'){ //预览文章的具体内容和封面
            var index = layui.layer.open({
                title : "添加文章",
                type : 2,
                content : "show?articleId="+data.articleId,
                success : function(layero, index){
                    var body = layui.layer.getChildFrame('body', index);
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回文章列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500)
                }
            })
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            })

        }else if (layEvent === 'withdraw') {
            console.log(data);
            layer.prompt({
                formType: 2,
                title: '请输入审核意见',
                area: ['400px', '60px'] //自定义文本域宽高
            }, function(value, index, elem){
                console.log(value);
                $.ajax({
                    url : "opinion",
                    type : "GET",
                    data : {
                        content:value,
                        articleId : data.articleId
                    },
                    success : function (result) {
                        if (result.code == 100){
                            tableIns.reload();
                            setTimeout(function(){
                                layer.msg("退回成功");
                            },500);
                        } else {
                            layer.msg("回复失败，未知异常联系管理员",{offset:'300px' });
                        }
                    }
                });
                layer.close(index);
            });

        }
    });

})